#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
# @Time    : 2022/2/10 15:26
# @Author  : daguo
# @File    : match_4.py
# @Function: 猿人学比赛第三题
"""
import re
import time
import hashlib
import requests
import base64
from lxml import etree

base64_nmber = {
    "": 0,
    "": 1,
    "": 2,
    "": 3,
    "": 4,
    "": 5,
    "": 6,
    "": 7,
    "": 8,
    "": 9
}
url = "https://match.yuanrenxue.com/api/match/4"
item = []
sum = 0
for i in range(1, 6):
    params = {
        "page": i
    }
    headers = {
        "user-agent": "yuanrenxue.project",
        "Cookie": f"sessionid=ip5n9gtjh9zo53qmxf7g7j4o0k2c3m3h",
        "Referer": "https://match.yuanrenxue.com/match/4",
        "Host": "match.yuanrenxue.com"
    }
    res = requests.get(url=url, headers=headers, params=params)
    if res.status_code == 200:
        print(res.url)

        btoa = base64.b64encode(str(res.json().get("key") + res.json().get("value")).encode("utf-8")).decode().strip("=")
        js_key = hashlib.md5(btoa.encode("utf-8")).hexdigest()
        html_text=f'<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body>{res.json().get("info")}</body></html>'
        info = etree.HTML(html_text)
        td_lxml=info.xpath("//td")
        count=0
        for td in td_lxml:
            img_left = {}
            imgs=[img for img in td if js_key not in img.xpath("./@class")[0]]
            for index,img in enumerate(imgs):
                im=float(re.findall("left:(.*?)px",img.xpath("./@style")[0])[0])/11.5
                img_src=base64_nmber.get(img.xpath("./@src")[0])
                img_left[int(index+im)]=img_src #规则是除以11.5以后再加上当前所在的坐标就是他最终所存在的位置
            count+= int("".join([str(img_left[i]) for i in range(len(img_left))]))
            print(count)
        sum+=count
    else:
        print("请求失败")
    time.sleep(0.75)
print(f"总和为{sum}")
